package com.lvsc.sdaplacefinder;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.lvsc.sdaplacefinder.models.Place;
import com.lvsc.sdaplacefinder.service.ImageDownloader;
import com.lvsc.sdaplacefinder.service.RESTService;
import com.lvsc.sdaplacefinder.util.JSONParserUtil;
import com.lvsc.sdaplacefinder.util.WebServiceUtil;
import com.lvsc.sdaplacefinder.widget.HorizontalListView;

public class PlaceActivity extends Activity {

	private Place place;
	private Dialog rankDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.item_place);
		Bundle data = getIntent().getExtras();

		place = (Place) data.getSerializable("place");

		setTitle(place.getName());

		final RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBarPlace);
		ratingBar.setRating(place.getRating());

		TextView desc = (TextView) findViewById(R.id.txtDescriptionPlace);
		desc.setText(place.getDescription());

		ratingBar.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_UP) {
					rankDialog = new Dialog(PlaceActivity.this,
							android.R.style.Theme_Holo_Light_Dialog);
					rankDialog.setContentView(R.layout.rating_dialog);
					rankDialog.setCancelable(true);

					Button btn = (Button) rankDialog.findViewById(R.id.voteBtn);
					btn.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {

							RatingBar ratingBarDialog = (RatingBar) rankDialog
									.findViewById(R.id.ratingBarPlaceDialog);

							int rating = (int) ratingBarDialog.getRating();

							Intent intent = new Intent(PlaceActivity.this,
									RESTService.class);
							intent.setData(Uri
									.parse(WebServiceUtil.SDA_REST_HOST
											+ "places/rate/" + place.getId()
											+ "/" + rating));

							intent.putExtra(RESTService.EXTRA_RESULT_RECEIVER,
									new ResultReceiver(new Handler()) {

										@Override
										protected void onReceiveResult(
												int resultCode,
												Bundle resultData) {
											if (resultData != null
													&& resultData
															.containsKey(RESTService.REST_RESULT)) {
												place = JSONParserUtil.parseObject(
														resultData
																.getString(RESTService.REST_RESULT),
														Place.class);
												ratingBar.setRating(place
														.getRating());
											} else {
												// onRESTResult(resultCode,
												// null);
											}
										}

									});

							// Here we send our Intent to our RESTService.
							startService(intent);

							rankDialog.dismiss();
						}
					});

					rankDialog.setTitle(place.getName());

					rankDialog.show();
				}
				return true;
			}
		});

		// Reference the Gallery view
		HorizontalListView g = (HorizontalListView) findViewById(R.id.horizontalListlView1);
		// Set the adapter to our custom adapter (below)
		g.setAdapter(new ImageAdapter(this));

		// Set a item click listener, and just Toast the clicked position
		g.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {

			}
		});
	}

	public void onClickNavigation(View v) {
		Intent i = new Intent(Intent.ACTION_VIEW,
				Uri.parse("google.navigation:q=" + place.getLatitude() + ","
						+ place.getLongitude()));
		startActivity(i);
	}

	public void onClickItineraire(View v) {

		Intent intent = new Intent(this, PathActivity.class);
		intent.putExtra("place", place);
		startActivity(intent);
		finish();
	}

	public class ImageAdapter extends BaseAdapter {
		private LayoutInflater inflater;

		public ImageAdapter(Context c) {
			TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
			a.recycle();

			inflater = LayoutInflater.from(c);
		}

		public int getCount() {
			return place.getImageCount();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public class ViewHolder {
			ImageView imageView;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = new ViewHolder();
			if (convertView == null) {

				convertView = inflater.inflate(R.layout.image_view, null);
				holder.imageView = (ImageView) convertView
						.findViewById(R.id.imageView);
				holder.imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
				holder.imageView.setLayoutParams(new LinearLayout.LayoutParams(
						130, 130));

				convertView.setTag(holder);

			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			String url = WebServiceUtil.SDA_REST_HOST + "upload/"
					+ place.getId() + "/" + position;
			new ImageDownloader(null).download(url, holder.imageView);

			return convertView;
		}
	}
}
